package com.sense.kukoo.common.wiget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sense.kukoo.common.R;
import com.sense.kukoo.common.utils.ApplicationContext;

public class BottomReportDialog extends Dialog {

    private View root;
    private View tv_btn_cancel;
    private View tv_btn_report;
    private View tv_btn_sexual;
    private View tv_btn_violent;
    private View tv_btn_hateful_speech;
    private View tv_btn_copyright_infringement;
    private View tv_btn_other;
    private View group_selection;

    public BottomReportDialog(@NonNull Context context) {
        super(context);
    }

    public BottomReportDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected BottomReportDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getContext().setTheme(R.style.bottom_dialog);
        setContentView(R.layout.common_bottom_report_dialog);
        //
        root = findViewById(R.id.root);
        tv_btn_cancel = findViewById(R.id.tv_btn_cancel);
        tv_btn_report = findViewById(R.id.tv_btn_report);
        tv_btn_sexual = findViewById(R.id.tv_btn_sexual);
        tv_btn_violent = findViewById(R.id.tv_btn_violent);
        tv_btn_hateful_speech = findViewById(R.id.tv_btn_hateful_speech);
        tv_btn_copyright_infringement = findViewById(R.id.tv_btn_copyright_infringement);
        tv_btn_other = findViewById(R.id.tv_btn_other);
        group_selection = findViewById(R.id.group_selection);
        //
        group_selection.setVisibility(View.GONE);
        //
        root.setOnClickListener(mOnClickListener);
        tv_btn_cancel.setOnClickListener(mOnClickListener);
        tv_btn_report.setOnClickListener(mOnClickListener);
        tv_btn_sexual.setOnClickListener(mOnClickListener);
        tv_btn_violent.setOnClickListener(mOnClickListener);
        tv_btn_hateful_speech.setOnClickListener(mOnClickListener);
        tv_btn_copyright_infringement.setOnClickListener(mOnClickListener);
        tv_btn_other.setOnClickListener(mOnClickListener);
    }

    @Override
    public void show() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        super.show();
        root.setTranslationY(ApplicationContext.dp2px(250));
        root.animate().translationY(0).setDuration(200).start();
    }

    @Override
    public void dismiss() {
        root.animate()
                .translationY(ApplicationContext.dp2px(250))
                .setDuration(200)
                .withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        _dismiss();
                    }
                })
                .start();
    }

    private void _dismiss(){
        super.dismiss();
    }

    private final View.OnClickListener mOnClickListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.root) {
                dismiss();
            }else if(id == R.id.tv_btn_cancel){
                dismiss();
            }else if(id == R.id.tv_btn_report){
                tv_btn_report.setVisibility(View.GONE);
                group_selection.setVisibility(View.VISIBLE);
            }else if(id == R.id.tv_btn_sexual){
                if (mListener!=null) {
                    mListener.onReport(1);
                }
                dismiss();
            }else if(id == R.id.tv_btn_violent){
                if (mListener!=null) {
                    mListener.onReport(2);
                }
                dismiss();
            }else if(id == R.id.tv_btn_hateful_speech){
                if (mListener!=null) {
                    mListener.onReport(3);
                }
                dismiss();
            }else if(id == R.id.tv_btn_copyright_infringement){
                if (mListener!=null) {
                    mListener.onReport(4);
                }
                dismiss();
            }else if(id == R.id.tv_btn_other){
                if (mListener!=null) {
                    mListener.onReport(5);
                }
                dismiss();
            }
        }
    };

    private ReportEventListener mListener;

    public interface ReportEventListener{
        void onReport(int type);
    }

    public BottomReportDialog setListener(ReportEventListener l){
        mListener = l;
        return this;
    }
}
